define(['jquery', 'zTree', 'text!../template/area.html', 'text!../template/areaInfo.html', 'text!../template/areaEdit.html', 'css!../css/area', 'common'], function ($, tree, tplArea, tplAreaInfo, tplAreaEdit) {
	var _area = {};
	var tpls = {
		'tplArea': _.template(tplArea),
		'tplAreaInfo': _.template(tplAreaInfo),
		'tplAreaEdit': _.template(tplAreaEdit)
	};
	var setting = {};
	_area.show = function () {
		let flg = 0;
		$('#contant').html(tpls.tplArea());
		setting = {
			check: {
				enable: true,
				chkStyle: "radio",
				radioType: 'all'
			},
			async: {
				enable: true,
				autoParam: ["id", "name=n", "level=lv"],
				otherParam: {
					"parentId": "320113"
				},
				type: "post",
			},
			edit: {
				enable: true,
				removeTitle: '删除',
				renameTitle: "修改"
			},
			view: {
				addHoverDom: addHoverDom,
				removeHoverDom: removeHoverDom,
				selectedMulti: false
			},
			callback: {
				onCheck: zTreeOnCheck,
				onClick: function (e, treeId, treeNode, clickFlag) {
					var zTree = $.fn.zTree.getZTreeObj("treeDemo");
					zTree.checkNode(treeNode, !treeNode.checked, true);
					zTreeOnCheck(e, treeId, treeNode)
				},
				onAsyncSuccess: function () {
					if ($("#treeDemo_2_a").length > 0 && flg == 0) {
						$("#treeDemo_2_a").click();
						flg = 1;
					}
				},
				//onRightClick: OnRightClick,
				beforeRemove: beforeRemove,
				beforeEditName: beforeEditName,
			}
		};
		setTree("#treeDemo", setting);
		$('.node_name').click(function (event) {
			$(this).parent().parent().find('.chk').click();
		});
		$('.areaBody').html(tpls.tplAreaInfo({
			message: {
				areaAdmin: {},
				areaInspect: {},
				info: {}
			}
		}));

		function zTreeOnCheck(event, treeId, treeNode) {
			Ajax("/xjrest/area/area_info?areaId=" + treeNode.id, areaBack);
		}
	}
	var newCount = 1;

	function addHoverDom(treeId, treeNode) {
		if (treeNode.type != 1) {
			return;
		}
		let _title = treeNode.id == 320113 ? '添加街道' : '添加社区';
		var sObj = $("#" + treeNode.tId + "_span");
		if (treeNode.editNameFlag || $("#addBtn_" + treeNode.tId).length > 0) return;
		var addStr = "<span class='button add' id='addBtn_" + treeNode.tId +
			"' title=" + _title + " onfocus='this.blur();'></span>";
		sObj.after(addStr);
		var btn = $("#addBtn_" + treeNode.tId);
		if (btn) btn.bind("click", function (event) {
			event.stopPropagation();
			layer.open({
				type: 1,
				title: false,
				closeBtn: 0,
				shadeClose: true,
				content: tpls.tplAreaEdit({
					message: ''
				})
			});
			$('#areaEditIn').click(function (event) {
				let parms = {};
				parms.areaType = 2;
				parms.areaName = $("#newAreaName").val();
				parms.parentId = treeNode.id;
				layer.closeAll();
				Ajax("/xjrest/area/area_save", function (data) {
					if (data.ret.success) {
						layer.msg('添加成功');
						setTree("#treeDemo", setting);
					}
				}, parms);
			});
			$('#areaEditOut').click(function (event) {
				layer.closeAll();
			});
			/*var zTree = $.fn.zTree.getZTreeObj("treeDemo");
			zTree.addNodes(treeNode, {id:(100 + newCount), pId:treeNode.id, name:"new node" + (newCount++)});
			return false;*/
		});
	}

	function removeHoverDom(treeId, treeNode) {
		$("#addBtn_" + treeNode.tId).unbind().remove();
	}

	function areaBack(data) {
		if (data.ret.success) {
			$('.areaBody').html(tpls.tplAreaInfo({
				message: data.ret
			}))
		}
	}

	function beforeRemove(treeId, treeNode) {
		layer.confirm('删除区域将会删除该区域下的所有社区以及巡检点,是否确定删除该区域？', {
			btn: ['确定', '取消'] //按钮
		}, function () {
			Ajax("/xjrest/area/area_delete?areaId=" + treeNode.id, function (data) {
				if (data.ret.success) {
					layer.msg('删除成功');
					setTree("#treeDemo", setting);
				}
			}, '', 'get', false);
		}, function () {

		});
		return false
	}

	function beforeEditName(treeId, treeNode) {
		layer.open({
			type: 1,
			title: false,
			closeBtn: 0,
			shadeClose: true,
			content: tpls.tplAreaEdit({
				message: treeNode.name
			})
		});
		$('#areaEditIn').click(function (event) {
			let parms = {};
			parms.areaName = $("#newAreaName").val();
			parms.areaId = treeNode.id;
			layer.closeAll();
			Ajax("/xjrest/area/area_save", function (data) {
				if (data.ret.success) {
					layer.msg('修改成功');
					setTree("#treeDemo", setting);
				}
			}, parms, 'get', false);
		});
		$('#areaEditOut').click(function (event) {
			layer.closeAll();
		});
		return false
	}

	return _area;
})